package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsAdminBrandService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DtsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsAdminBrandServiceImpl implements DtsAdminBrandService {

    @Autowired
    DtsBrandMapper brandMapper;
    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    StatMapper statMapper;

    @Override
    public List<Integer> selectId(Integer id) {
        LambdaQueryWrapper<DtsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getAdminId,id);
        qw.select(DtsBrand::getId);
        List<DtsBrand> brands = brandMapper.selectList(qw);
        List<Integer> brandIds = brands.stream().map(dtsBrand -> {
            return dtsBrand.getId();
        }).collect(Collectors.toList());
        return brandIds;
    }

    @Override
    public List<Map<String, Object>> queryBrandList() {
        LambdaQueryWrapper<DtsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DtsBrand::getDeleted,false);
        List<DtsBrand> dtsBrands = brandMapper.selectList(queryWrapper);
        List<Map<String, Object>> brandList = dtsBrands.stream().map(dtsBrand -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsBrand.getId());
            map.put("label", dtsBrand.getName());
            return map;
        }).collect(Collectors.toList());
        return brandList;
    }

    @Override
    public IPage<DtsBrandVo> queryPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsBrand> iPage = new Page<>(page,limit);
        QueryWrapper<DtsBrand> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        if(!StringUtils.isEmpty(id)){
            qw.like("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        qw.eq("deleted",false);

        brandMapper.selectPage(iPage,qw);


        IPage<DtsBrandVo> iPage1 = new Page<>();
        List<DtsBrandVo> dtsBrandVo = new ArrayList<>();
        List<DtsBrand> records = iPage.getRecords();
        for (DtsBrand record : records) {
            DtsBrandVo dtsBrandVo1 = new DtsBrandVo();
            dtsBrandVo1.setId(record.getId());
            dtsBrandVo1.setName(record.getName());
            dtsBrandVo1.setDesc(record.getDesc());
            dtsBrandVo1.setPicUrl(record.getPicUrl());
            dtsBrandVo1.setSortOrder(record.getSortOrder());
            dtsBrandVo1.setFloorPrice(record.getFloorPrice());
            dtsBrandVo1.setAddTime(record.getAddTime());
            dtsBrandVo1.setUpdateTime(record.getUpdateTime());
            dtsBrandVo1.setShareUrl(record.getShareUrl());
            dtsBrandVo1.setAdminId(record.getAdminId());
            dtsBrandVo1.setAdminId(record.getAdminId());
            dtsBrandVo1.setDeleted(record.getDeleted());
            dtsBrandVo1.setCommpany(record.getCommpany());
            dtsBrandVo1.setAutoUpdateGood(record.getAutoUpdateGood());
            dtsBrandVo1.setDefaultCategoryId(record.getDefaultCategoryId());
            dtsBrandVo1.setDefaultPages(record.getDefaultPages());
            dtsBrandVo1.setAddPrecent(record.getAddPrecent());

            Integer defaultCategoryId = record.getDefaultCategoryId();
            Map<String,Object> map = statMapper.queryPidAndId(defaultCategoryId);
            Integer pid = (Integer) map.get("pid");
            Integer id1 = (Integer) map.get("id");
            Integer[] integers = new Integer[]{id1,pid};
            dtsBrandVo1.setCategoryIds(integers);
            dtsBrandVo.add(dtsBrandVo1);
        }
        iPage1.setRecords(dtsBrandVo);
        iPage1.setTotal(iPage.getTotal());
        return iPage1;
    }

    @Override
    public DtsBrand insert(DtsBrand dtsBrand) {
        dtsBrand.setDeleted(false);
        brandMapper.insert(dtsBrand);
        return dtsBrand;
    }

    @Override
    public DtsBrand update(DtsBrand dtsBrand) {
        brandMapper.updateById(dtsBrand);
        return dtsBrand;
    }

    @Override
    public Object delete(DtsBrand dtsBrand) {
        Integer brandId = dtsBrand.getId();
        LambdaQueryWrapper<DtsGoods> qw= new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getBrandId,brandId);
        qw.eq(DtsGoods::getDeleted,false);
        List<DtsGoods> dtsGoods = goodsMapper.selectList(qw);
        if(dtsGoods.size() != 0){
            return ResponseUtil.fail(-1,"该品牌商下还有商品未删除");
        }

        brandMapper.deleteById(dtsBrand);
        return ResponseUtil.ok();
    }


}
